package com.swak.sql.parsing.engine.clause;

import com.swak.parser.lexer.LexerEngine;
import com.swak.parser.lexer.token.DefaultKeyword;
import com.swak.parser.lexer.token.Symbol;
import com.swak.parser.lexer.token.Token;
import com.swak.sql.parsing.context.Statement;
import com.swak.sql.parsing.context.Table;

/**
 * Select 语句的解析
 * 
 * @author DELL
 */
public class SelectClauseParser {

	Statement statement;
	LexerEngine lexer;

	public SelectClauseParser(Statement statement, LexerEngine lexer) {
		this.statement = statement;
		this.lexer = lexer;
	}

	public Statement parse() {
		this.lexer.nextToken();
		this.parseDistinct();
		this.parseSelectItems();
		return statement;
	}

	protected void parseDistinct() {
		if (this.lexer.equalAny(DefaultKeyword.DISTINCT)) {
			this.lexer.nextToken();
		}
	}

	protected void parseSelectItems() {
		do {
		} while (this.lexer.skipIfEqual(Symbol.COMMA));
	}

	protected void parseFrom() {
		if (this.lexer.equalAny(DefaultKeyword.FROM)) {
			do {
				this.lexer.nextToken();
				Token token = this.lexer.getCurrentToken();
				this.statement.getTables()
						.add(new Table().setName(token.getLiterals()).setEndPosition(token.getEndPosition())
								.setStartPosition(token.getEndPosition() - token.getLiterals().length()));
			} while (this.lexer.skipIfEqual(Symbol.COMMA));
		}
	}
}
